<div class="page">
	<div class="page-header">
		<div class="pull-left">
			<i class="icon-awesome-file-alt icon-large"></i>
			<span class="divider"></span>
			<div class="title">
				<h3><?php echo __('Empréstimos de Equipamentos');?></h3>
				<small><?php echo ('Criar novo empréstimo, editar, devolver e renovar');?></small>
			</div>
		</div>
		<div class="pull-right">
			<?php echo $this->Html->link('<i class="icon-awesome-plus icon-white"></i>'.__('Novo'), array('action' => 'add'), array('escape' => false, 'class' => 'btn btn-primary btn-square', 'title' => 'Criar Empréstimo')); ?>
		</div>
	</div>
	<div class="page-content">
		<?php echo $this->Form->create('Filter', array('class' => 'well well-small')); ?>
		<fieldset>
		<legend>Filtrar por</legend>
		<div class="row-fluid">
			<div class="control-group span4">
				<?php echo $this->Form->Label('projeto_id', 'Projeto', array('class' => 'control-label'));	?>
				<div class="controls">
					<?php echo $this->Form->input('projeto_id', array('options' => $projetos, 'empty' => 'Todos...', 'label' => false, 'div' => false, 'class' => 'span12'));	?>
				</div>
			</div>
			<div class="control-group span4">
				<?php echo $this->Form->Label('autorizador_id', 'Professour autorizador', array('class' => 'control-label'));	?>
				<div class="controls">
					<?php echo $this->Form->input('autorizador_id', array('options' => $autorizadors, 'empty' => 'Todos...', 'label' => false, 'div' => false, 'class' => 'span12'));	?>
				</div>
			</div>
			<div class="control-group span4">
				<?php echo $this->Form->Label('aluno_id', 'Solicitante', array('class' => 'control-label'));	?>
				<div class="controls">
					<?php echo $this->Form->input('aluno_id', array('options' => $projetos, 'empty' => 'Todos...', 'label' => false, 'div' => false, 'class' => 'span12'));	?>
				</div>
			</div>
		</div>
		<div class="row-fluid">
			<div class="control-group span4">
				<?php echo $this->Form->Label('data_inicio', 'Período', array('class' => 'control-label'));	?>
				<div class="controls">
					<div class="input-append span6">
						<?php $this->request->data['Filter']['data_inicio'] = (!empty($this->request->data['Filter']['data_inicio'])) ? date('d/m/Y', strtotime($this->request->data['Filter']['data_inicio'])) : ''; ?>
						<?php echo $this->Form->input('data_inicio', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span10', 'required' => false, 'error' => false)); ?>
						<span class="add-on"><i class="icon-calendar"></i></span>
					</div>
					<div class="input-append span6">
						<?php $this->request->data['Filter']['data_entrega'] = (!empty($this->request->data['Filter']['data_entrega'])) ? date('d/m/Y', strtotime($this->request->data['Filter']['data_entrega'])) : ''; ?>	
						<?php echo $this->Form->input('data_entrega', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span10', 'required' => false, 'error' => false)); ?>
						<span class="add-on"><i class="icon-calendar"></i></span>
					</div>
				</div>
			</div>
			<div class="control-group span4">
				<?php echo $this->Form->Label('situacao', 'Situação', array('class' => 'control-label'));	?>
				<div class="controls">
					<?php echo $this->Form->input('situacao', array('options' => $situacaos, 'empty' => 'Todos...', 'label' => false, 'div' => false, 'class' => 'span12', 'required' => false, 'error' => false));	?>
				</div>
			</div>
		</div>
		<div class="row-fluid">
			<div class="control-group">
				<?php echo $this->Form->button('<i class="icon-search icon-white"></i> Filtrar', array('type' => 'submit', 'div' => false, 'class' => 'btn btn-primary', 'escape' => false)); ?>
			</div>
		</div>
		</fieldset>
		<?php echo $this->Form->end(); ?>
	
		<table class="table table-bordered table-striped table-condensed">
		<thead>
		<tr>
			<th width="7%"><?php echo $this->Paginator->sort('id', 'Nº');?></th>
			<th width="15%"><?php echo $this->Paginator->sort('projeto_id', 'Projeto');?></th>
			<th><?php echo $this->Paginator->sort('aluno_id', 'Aluno');?></th>
			<th width="15%"><?php echo $this->Paginator->sort('Autorizador.nome', 'Professor');?></th>
			<th width="10%"><?php echo $this->Paginator->sort('data_inicio', 'Início');?></th>
			<th width="10%"><?php echo $this->Paginator->sort('data_entrega', 'Entrega');?></th>
			<th width="12%"><?php echo $this->Paginator->sort('situacao', 'Situação');?></th>
			<th class="actions"><?php echo __('Ações');?></th>
		</tr>
		</thead>
		<tbody>
<?php
		foreach ($emprestimos as $emprestimo): ?>
		<tr>
			<td><?php echo h($emprestimo['Emprestimo']['id']); ?>&nbsp;</td>
			<td class="link"><?php echo $this->Html->link($emprestimo['Projeto']['nome'], array('controller' => 'projetos', 'action' => 'view', $emprestimo['Projeto']['id']), array('title' => 'Dados do Projeto')); ?></td>
			<td class="link"><?php echo $this->Html->link($emprestimo['Aluno']['nome'], array('controller' => 'alunos', 'action' => 'view', $emprestimo['Aluno']['id']), array('title' => 'Dados do Aluno')); ?></td>
			<td class="link"><?php echo $this->Html->link($emprestimo['Autorizador']['nome'], array('controller' => 'usuarios', 'action' => 'view', $emprestimo['Autorizador']['id']), array('title' => 'Dados do Professor')); ?></td>
			<td><?php echo h(date('d/m/Y', strtotime($emprestimo['Emprestimo']['data_inicio']))); ?>&nbsp;</td>
			<td><?php
				if(!empty($emprestimo['Emprestimo']['data_entrega'])) 
					echo h(date('d/m/Y', strtotime($emprestimo['Emprestimo']['data_entrega']))); ?>&nbsp;</td>
			<td><?php 
				if (array_key_exists($emprestimo['Emprestimo']['situacao'], $situacaos)) { 
					switch ($emprestimo['Emprestimo']['situacao']) {
						case 'A':
							$class = 'label-warning';
							break;
						case 'E':
							$class = 'label-info';
							break;
						case 'F':
							$class = 'label-success';
							break;
						default: 
							$class = '';
					} ?>
					<span class="label <?php echo $class; ?>"><?php echo $situacaos[$emprestimo['Emprestimo']['situacao']]; ?></span>
				<?php } ?>
			</td>
			<td class="actions">
				<?php echo (in_array($emprestimo['Emprestimo']['situacao'], array_keys($situacaos))) ? $this->Html->link('<i class="icon-edit icon-white"></i>', array('action' => 'edit', $emprestimo['Emprestimo']['id']), array('escape' => false, 'class' => 'btn btn-primary btn-mini', 'title' => 'Ver Detalhes')) : ''; ?>
				<?php echo (in_array($emprestimo['Emprestimo']['situacao'], array_keys($situacaos))) ? $this->Form->postLink('<i class="icon-trash icon-white"></i>', array('action' => 'delete', $emprestimo['Emprestimo']['id']), array('escape' => false, 'class' => 'btn btn-danger btn-mini'), __('Are you sure you want to delete # %s?', $emprestimo['Emprestimo']['id'])) : ''; ?>
			</td>
		</tr>
<?php endforeach; ?>
		</tbody>
		</table>
		<?php echo $this->element('pagination'); ?>	
	</div>
</div>

<?php echo $this->element('form-modal'); ?>	
<script type="text/javascript">
	$(document).ready(function () {
		$('#FilterDataInicio').mask('99/99/9999').datepicker();
		$('#FilterDataEntrega').mask('99/99/9999').datepicker();
		$('.actions a, button').tooltip();

		$('td.link > a').click(function(event) {
			event.preventDefault();
			
			var url = $(this).attr('href');
			var title = $(this).attr('title');
			var container = $('#form-modal');
			var header = container.children('.modal-header');
			var content = container.children('.modal-body');
			var footer = container.children('.modal-footer');
			
			$("#ajax-indicator").show();
			content.empty();
            $.get(url, function (data) {
            	header.children('h4:first').text(title);
				if (url.indexOf('add') >= 0) {
					footer.children('button:last-child').show();
				} else if (url.indexOf('edit') >= 0) {
					footer.children('button:last-child').show();
				} else if (url.indexOf('view') >= 0) {
					footer.children('button:last-child').hide();
				}
				content.html(data);
                container.modal();  
            })
            .always(function(){
            	$("#ajax-indicator").hide();
            });
            
		});
	});
</script>